package com.yy.bootdemo.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.bootdemo.admin.dto.MenDto;
import com.yy.bootdemo.admin.entity.MyMenu;
import com.yy.bootdemo.admin.service.MenuService;
import com.yy.bootdemo.common.utils.MenuTree1;
import com.yy.bootdemo.security.utils.PageUtils;
import com.yy.bootdemo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author codermy
 * @createTime 2020/7/10
 */
/*@RestController
@RequestMapping("/api/menu")
@Api(tags = "系统：菜单管理")*/
@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private MenuService menuService;


//    @ApiOperation(value = "菜单列表")
     @PreAuthorize("hasAnyAuthority('menu:list')")
//    @RequestMapping("/list")..................................
//    @MyLog("查询菜单")
    @RequestMapping("/list")
    public R getMenuAll(@RequestBody PageUtils params){
        // 条件构造器
        QueryWrapper<MyMenu> userQuery = Wrappers.query();
        //userQuery.ge("age", 26);

        Page<MyMenu> page = new Page<>(params.getCurrent(), params.getSize());

        IPage<MyMenu> userPage = menuService.page(page, userQuery);
        System.out.println("总页数：" + userPage.getPages());
        System.out.println("总记录数：" + userPage.getTotal());
        System.out.println();
        List<MyMenu> userList = userPage.getRecords();
        userList.forEach(System.out::println);
        return R.ok(userPage);
    }
    @GetMapping("getMenuList")
    public R getMenuList(){
        // 条件构造器
        QueryWrapper<MyMenu> userQuery = Wrappers.query();

         userQuery.in("type",0,1);
        List<MyMenu> list=  menuService.list(userQuery);
        MenuTree1 menuTree1=new  MenuTree1();

        List<Map<String,Object>> li=  menuTree1.tree2(0,list);
        return R.ok(li);
    }


    //根据父节点获取全部子节点
   /* public static List<Menu> getChildren(Integer pid, List<MyMenu> nodeList){
        List<Menu> resultList = new ArrayList<>();
        if (null != nodeList && nodeList.size() > 0){
                for (MyMenu layer : nodeList) {
                    if(pid.equals(layer.getParentId())){
                        Menu menu=new Menu();
                        menu.setMenuId(layer.getMenuId());
                        menu.setParentId(layer.getParentId());
                       // menu.setList(nodeList);
                        //添加子级节点
                        resultList.add(menu);
                        //递归获取深层节点
                        resultList.addAll(getChildren(layer.getMenuId(),nodeList));
                    }
                }

        }
        return resultList;
    }*/



    @GetMapping("getById")
    public R getMenu(@RequestParam Integer menuId){
        return R.ok(menuService.getById(menuId));
    }

    @PostMapping("save")
    public R save(@RequestBody MyMenu myMenu){
        menuService.save(myMenu);
        return R.ok();
    }

    @PostMapping("updateById")
    public R updateById(@RequestBody MyMenu myMenu){
        menuService.updateById(myMenu);
        return R.ok();
    }

    @GetMapping("/ebuild/{roleId}")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('role:add','role:edit')")
    public R buildMenuAllByRoleId(@PathVariable Integer roleId){
        List<MenDto> menuAll =menuService.buildMenuAllByRoleId(roleId);
        return R.ok(menuAll);
    }

    @GetMapping("/build")
    @ResponseBody
    public R buildMenuAll(){
        List<MenDto> menuAll =menuService.buildMenuAll();
        return R.ok(menuAll);
    }

   /* @GetMapping("/build")
    @ResponseBody
    @ApiOperation(value = "绘制菜单树")
    @PreAuthorize("hasAnyAuthority('menu:add','menu:edit')")
    @MyLog("绘制菜单树")
    public R buildMenuAll(){
        List<MenuDto> menuAll =menuService.buildMenuAll();
        return R.ok().data(menuAll);
    }

    @GetMapping("/ebuild/{roleId}")
    @ResponseBody
    @ApiOperation(value = "通过id绘制菜单树")
    @PreAuthorize("hasAnyAuthority('role:add','role:edit')")
    @MyLog("通过id绘制菜单树")
    public R buildMenuAllByRoleId(@PathVariable Integer roleId){
        List<MenuDto> menuAll =menuService.buildMenuAllByRoleId(roleId);
        return R.ok().data(menuAll);
    }

    @GetMapping(value = "/edit")
    @ApiOperation(value = "修改菜单页面")
    @PreAuthorize("hasAnyAuthority('menu:edit')")
    public String editPermission(Model model, MyMenu myMenu) {
        model.addAttribute("myMenu",menuService.getMenuById(myMenu.getMenuId()));
        return "system/menu/menu-edit";
    }

    @PutMapping
    @ResponseBody
    @ApiOperation(value = "修改菜单")
    @PreAuthorize("hasAnyAuthority('menu:edit')")
    @MyLog("修改菜单")
    public R updateMenu(@RequestBody MyMenu menu) {
        return menuService.updateMenu(menu);
    }


    @GetMapping(value = "/add")
    @ApiOperation(value = "添加菜单页面")
    @PreAuthorize("hasAnyAuthority('menu:add')")
    public String addMenu(Model model) {
        model.addAttribute("myMenu",new MyMenu());
        return "system/menu/menu-add";
    }

    @PostMapping
    @ResponseBody
    @ApiOperation(value = "添加菜单")
    @PreAuthorize("hasAnyAuthority('menu:add')")
    @MyLog("添加菜单")
    public R<MyMenu> savePermission(@RequestBody MyMenu myMenu) {
        return menuService.save(myMenu);
    }


    *//**
     * @param menuId
     * @return
     *//*
    @DeleteMapping
    @ResponseBody
    @ApiOperation(value = "删除菜单")
    @PreAuthorize("hasAnyAuthority('menu:del')")
    @MyLog("删除菜单")
    public R deleteMenu(Integer menuId) {
        return menuService.delete(menuId);
    }
*/
}
